package com.tz.crm.service.impl;

import com.tz.crm.dao.DepartmentDao;
import com.tz.crm.entity.Department;
import com.tz.crm.entity.User;
import com.tz.crm.service.DepartmentService;
import com.tz.crm.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

;

/**
 * 查找部门信息以及员工信息服务类
 */
@Service
@Transactional
public class DepartmentServiceImpl extends BaseServiceImpl<Department, String> implements DepartmentService {

    @Autowired
    private DepartmentDao departmentDao;

    /**
     * 有参构造
     *
     * @param departmentDao 注入的组件
     */
    @Autowired
    public DepartmentServiceImpl(DepartmentDao departmentDao) {
        this.departmentDao = departmentDao;
        super.setBaseDao(departmentDao);
    }

    @Override
    public void update(Department dept) {
        //Depart get...
        Department dept1 = departmentDao.getById(dept.getId());
        //set...dept ==> Depart
        dept1.setDeptName(dept.getDeptName());
        dept1.setRemark(dept.getRemark());
        dept1.setParent(dept.getParent());
        departmentDao.update(dept1);
    }

    @Override
    public Department get(String name) {
        return departmentDao.get(name);
    }

    @Override
    public List<User> getByDepartmentName(String name, Pager pager) {
        return departmentDao.getByDepartmentName(name, pager);
    }

    @Override
    public Department getById(String id) {
        return departmentDao.getById(id);
    }

    @Override
    public Department getDeptWithChildren(String parentName) {
        return departmentDao.getDeptWithChildren(parentName);
    }

    @Override
    public Department getRootWithChildren() {
        Department root = departmentDao.getRoot();
        return this.getDeptWithChildren(root.getDeptName());
    }

}
